package student.aistar.util;

import org.apache.commons.dbcp.BasicDataSource;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 连接池[两个jar]设置参数而已
 */
public class PoolUtil {
    private static volatile PoolUtil instance = null;

    private BasicDataSource basicDataSource;

    private PoolUtil(){
        //进行一些连接池的配置
        basicDataSource = new BasicDataSource();
        basicDataSource.setUrl("jdbc:mysql://localhost:3306/m503?useSSL=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("Abc1234!");

        //设置参数
        basicDataSource.setInitialSize(Integer.valueOf(DBPropUtil.getValue("initialSize")));
        basicDataSource.setMinIdle(Integer.valueOf(DBPropUtil.getValue("minIdle")));
        basicDataSource.setMaxIdle(Integer.valueOf(DBPropUtil.getValue("maxIdle")));
        basicDataSource.setMaxActive(Integer.valueOf(DBPropUtil.getValue("maxActive")));
        basicDataSource.setMaxWait(Long.valueOf(DBPropUtil.getValue("maxWait")));
    }

    public static PoolUtil getInstance(){
        if(null == instance){
            synchronized (PoolUtil.class){
                if(null == instance){
                    instance = new PoolUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 获取连接
     * @return
     * @throws SQLException
     */
    public Connection getConnection() throws SQLException {
        return basicDataSource.getConnection();
    }
}
